package de.tum.in.eist.poll.client;

import com.googlecode.gchart.client.GChart;

import de.tum.in.eist.poll.pub.shared.Lecture;

/**
 * This class is used to show the satisfaction-statistics of one lecture.
 */
public class Chart extends GChart {
	Stats data;
	Lecture lecture;
	int x;
	int y;

	/**
	 * Initializes a Chart-Object
	 * 
	 * @param lecture is the lecture the current chart is about
	 * @param x is the width of the cart
	 * @param y is the height of the cart
	 * @param admin is an Integer, which proves, that the user is an admin
	 * @param view is an object which contains the AdminView
	 */
	public Chart(final Lecture lecture, final int x, final int y, int admin, AdminStatisticView view) {
		this.lecture = lecture;
		this.data = new Stats(lecture, this, admin, view);
		this.x = x;
		this.y = y;
	}
	
	/**
	 * draws a chart, which shows the lecture`s statistics
	 */
	public void draw(){
		if(data.getOpinions() == null){
			setChartTitle("For the Lecture " + lecture.getName() + " are no opinions available.");
			setChartSize(x, 10);
			getXAxis().setAxisVisible(false);
			getXAxis().clearTicks();
			getYAxis().setAxisVisible(false);
			getYAxis().clearTicks();
			update();
		}
		else
			buildChart(x, y);
	}

	/**
	 * Builds the chart based on the given data 
	 * 
	 * @param x is the charts width
	 * @param y is the charts height
	 */
	private void buildChart(int x, int y) {
		String[] labels = {"Very unsatisfied", "Unsatisfied", "Neutral", "Satisfied", "Very Satisfied"};
		String[] colors = {"red", "orange", "yellow", "GreenYellow", "green"};
		setChartTitle("Statistics of Lecture " + this.lecture.getName());
		setChartSize(x, y);
		for (int i = 0; i < data.getOpinions().length; i++){
			addCurve();
			getCurve().addPoint(i+1, data.getOpinions()[i]);
			getCurve().getPoint().setAnnotationText(labels[i]);
			getCurve().getPoint().setAnnotationLocation(DEFAULT_VBAR_BASELINE_HOVER_LOCATION);
			getCurve().getSymbol().setBackgroundColor(colors[i]);
			getCurve().getSymbol().setSymbolType(SymbolType.VBAR_SOUTHWEST);
			getCurve().getSymbol().setBorderColor("black");
			getCurve().getSymbol().setModelWidth(0.2);
			getCurve().getSymbol().setHovertextTemplate("${y}");
		}
		getXAxis().setAxisVisible(false);
		getXAxis().clearTicks();
		getYAxis().setAxisVisible(false);
		getYAxis().clearTicks();
		this.update();
	}
	
	/**
	 * @return A Stats object, which contains all statistical data of this chart
	 */
	public Stats getData(){
		return data;
	}
}
